/*-----------------------------------------------------------------------------
 * @file   hal_irq.h
 * @brief  interface for IRQ module operations
 * @author WLH<wanglehui8357@163.com> Copyright (C) 2021~2099.
 ----------------------------------------------------------------------------*/
#ifndef HAL_IRQ_H
#define HAL_IRQ_H

#include "globaldef.h"

BEGIN_DECLS

/**
 * \brief Helper macro for entering critical sections
 *
 * This macro is recommended to be used instead of a direct call
 * hal_irq_enter_critical() function to enter critical
 * sections. No semicolon is required after the macro.
 *
 * \section atomic_usage Usage Example
 * \code
 * CRITICAL_SECTION_ENTER
 * Critical code
 * CRITICAL_SECTION_LEAVE
 * \endcode
 */
#define CRITICAL_SECTION_ENTER \
    {                          \
        vU32 __atomic;         \
        hal_irq_enter_critical(&__atomic);

/**
 * \brief Helper macro for leaving critical sections
 *
 * This macro is recommended to be used instead of a direct call
 * hal_irq_exit_critical() function to leave critical
 * sections. No semicolon is required after the macro.
 */
#define CRITICAL_SECTION_LEAVE        \
    hal_irq_exit_critical(&__atomic); \
    }

/*-----------------------------------------------------------------------------
 * public functions declaration
-----------------------------------------------------------------------------*/
/**
 * @brief Disable interrupts, enter critical section
 *
 * Disables global interrupts. Supports nested critical sections,
 * so that global interrupts are only re-enabled
 * upon leaving the outermost nested critical section.
 *
 * \param[out] atomic The pointer to a variable to store the value of global
 * interrupt enable flag
 */
void hal_irq_enter_critical(vU32 *atomic);
/**
 * @brief Exit atomic section
 *
 * Enables global interrupts. Supports nested critical sections,
 * so that global interrupts are only re-enabled
 * upon leaving the outermost nested critical section.
 *
 * \param[in] atomic The pointer to a variable, which stores the latest stored
 * value of the global interrupt enable flag
 */
void hal_irq_exit_critical(vU32 *atomic);

END_DECLS

#endif //~HAL_IRQ_H
